package com.itcast.controller;

import com.aliyuncs.exceptions.ClientException;
import com.itcast.constant.MessageConstant;
import com.itcast.constant.RedisMessageConstant;
import com.itcast.entity.Result;
import com.itcast.utils.SMSUtils;
import com.itcast.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

/**
 * @className: ValidateCodeControlller
 * @description: 验证码操作
 * @author: Jng
 * @date: 2022/4/14
 **/
@RestController
@RequestMapping("/validateCode")
public class ValidateCodeControlller {

    @Autowired
    JedisPool jedisPool;

    //用户预约体检发送验证码
    @RequestMapping("/send4Login")
    public Result send4Login(String telephone){
        // 随机生成4位验证码数字
        Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
        try {
            //给用户发送验证码
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,validateCode.toString());
        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        //将验证码存入redis缓存中去
        jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_ORDER,5*60,validateCode.toString());
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }


}
